/**
 * 
 */
package ch.connection;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;

import javax.net.ServerSocketFactory;
import javax.net.ssl.SSLServerSocketFactory;

/** Classe per la gestione del server POP
 * 
 * @author Giovanni Franza - GPLv3 - 8/6/2011
 *
 */

public class SSL extends Thread {
	private HashMap<String,ServerHandler> serverhandler = new HashMap<String,ServerHandler>();

	/** Construttore .
	 * 
	 */

	public SSL() {
	}

	/** getter della procedura finale.
	 *
	 */

	public ServerHandler getHandler( String service ) {
		return serverhandler.get( service );
	}

	/** setter della procedura finale.
	 *
	 * @param service - nome associato alla procedura.
	 * @param serverhandler - procedura .
	 * 
	 */
	
	public void register( String service, ServerHandler serverhandler ) {
		this.serverhandler.put( service, serverhandler );
	}

	/** Il run fa tutto il lavoro.
	 *
	 */	

	public void run() {
		try {
			System.out.println( "Welcome to the stupid, quick and dirty https server" ); 
			System.out.println( "Licenced as per GPLv3 by HB9EIK - Giovanni" ); 
			// unprivileged port to be able to launch as not root
		    int port = 1443;
		    // SSL server creation
		    ServerSocketFactory ssocketFactory = SSLServerSocketFactory.getDefault();
			ServerSocket ssocket = ssocketFactory.createServerSocket(port);
			// Serving requests (serialized) 
			for( ;; ) {
			    // Listen for connections
			    Socket socket = ssocket.accept();	
			    // Handle connections
			    SSLWorker sslworker = new SSLWorker( socket, this );
			    sslworker.start();
			}
		} catch(IOException e) {
			System.out.println(e);
		}
	}
}
